#include<bits/stdc++.h>

using namespace std;

const int N = 1010;

int n, m, k;
int dis[N];
int g[N][N];
bool st[N];
int ret = 0;

void dijkstra() {
	for(int i = 1; i <= n; ++ i ) {
		int t = -1;
		for(int j = 1; j <= n; ++ j ) {
			if((t == -1 || dis[j] < dis[t]) && !st[j]) {
				t = j;
			}
		}
		st[t] = true;
		for(int j = 1; j <= n; ++ j ) {
			dis[j] = min(dis[j], dis[t] + g[t][j]);
		}
	}
}

signed main() {
	
	memset(g, 0x3f, sizeof g);
	memset(dis, 0x3f, sizeof dis);
	dis[1] = 0;
	
	cin >> n >> m;
	for(int i = 1; i <= n; ++ i ) {
		g[i][i] = 0;
	}
	for(int i = 1; i <= m; ++ i ) {
		int x, y, v;
		cin >> x >> y >> v;
		g[x][y] = g[y][x] = min(g[x][y], v);
	}
	dijkstra();
	for(int i = 1; i <= n; ++ i ) {
		ret = max(ret, dis[i]);
	}
	if(ret == 0x3f3f3f3f)ret = -1;
	cout << ret << endl;
	return 0;
}

